package com.yuzhenxin.coursesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.framework.common.util.ExcelUtils;
import com.yuzhenxin.coursesystem.controller.course.vo.CourseQueryReqVO;
import com.yuzhenxin.coursesystem.entity.CourseDO;
import com.yuzhenxin.coursesystem.mapper.CourseMapper;
import com.yuzhenxin.coursesystem.service.CourseService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CourseDO> implements CourseService {

    @Resource
    private CourseMapper courseMapper;

    @Override
    public IPage<CourseDO> selectPage(CourseQueryReqVO reqDto) {
        Page<CourseDO> page = new Page<>(reqDto.getPageNo(), reqDto.getPageSize());
        Integer courseNumber = reqDto.getCourseNumber();
        String courseName = reqDto.getCourseName();
        LambdaQueryWrapper<CourseDO> queryWrapper = Wrappers.<CourseDO>lambdaQuery()
                .like(ObjectUtils.isNotEmpty(courseNumber), CourseDO::getNumber, courseNumber)
                .like(ObjectUtils.isNotEmpty(courseName), CourseDO::getName, courseName);
        return courseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void exportData(HttpServletResponse response) {
        String fileName = "CourseEntity";
        List<CourseDO> courseEntityList = courseMapper.selectList(null);
        ExcelUtils.export(fileName, "sheet1", courseEntityList, CourseDO.class, response);
    }

    @Override
    public List<Map<String, Object>> selectCourseGroupByCredit() {
        return courseMapper.selectCourseGroupByCredit();
    }

}
